let game = document.getElementById("game");
game.width = document.documentElement.clientWidth;
let ctx = game.getContext("2d");
let progress = document.querySelector(".progress");
let progressBox = document.querySelector(".progress-box");
//第一步：获取所有的图片的数量
let totalCount = Object.values(resList).flat().length;
let loadCount = 0;
for (let propertyName in resList) {
    if (Array.isArray(resList[propertyName])) {
        for (let i = 0; i < resList[propertyName].length; i++) {
            let img = new Image();
            img.src = resList[propertyName][i];
            resObj[propertyName].push(img);
            img.onload = function () {
                loadCount++;
                progress.style.backgroundSize = `${loadCount / totalCount * 100}% 100%`;
                if (loadCount == totalCount) {
                    console.log("资源加载完成");
                    initGame();
                }
            }
        }
    }
}

let bg = null;
let hero = null;
let keySet = new Set();
let enemyList = [];
let btnStart = document.querySelector(".btnStart");
let textBox = document.querySelector(".textBox");
let bgm = document.querySelector("#bgm");
let startBox = document.querySelector(".startBox");
let box = document.querySelector(".box");
let score = 0;
function initGame() {
    bg = new Background();
    bg.draw(ctx);
    progressBox.remove();
    box.style.transform = "translateY(0)";
    box.style.opacity = "1";

    btnStart.onclick = function () {
        textBox.style.opacity = 1;
        textBox.style.animationPlayState = "running";
        bgm.play();
        btnStart.remove();
    }
    textBox.onanimationend = function () {
        startBox.remove();
        startGame();
    }

}

function startGame() {
    hero = new Hero();
    setInterval(() => {
        addEnemy();
        bg.draw(ctx);
        hero.draw(ctx);
        for (let i = enemyList.length - 1; i >= 0; i--) {
            enemyList[i].draw(ctx);
        }
        ctx.font = "28px bold 微软雅黑";
        ctx.textAlign = "right";
        ctx.textBaseline = "top";
        ctx.fillText(`得分：${score}`, document.documentElement.clientWidth - 50, 10);
    }, 50);
}

document.onkeydown = function (event) {
    console.log(event.keyCode);
    //a:65 d:68 w:87 d:83
    if ([65, 68, 87, 83].includes(event.keyCode)) {
        keySet.add(event.keyCode)
    }
    else if (event.keyCode === 74) {
        //j键，攻击
        if (hero) {
            hero.isAttack = true;
            hero.playAttackMuskc();
            checkHeroAndEnemyCrash();
        }
    }
    else if (event.keyCode === 75) {
        //k键 大招
        if (hero && (hero.mp >= hero.totalMp)) {
            hero.isBoom = true;
            hero.playBoomMusic();
            checkHeroAndEnemyCrash();
            hero.mp = 0;
        }
    }
}

document.onkeyup = function (event) {
    if ([65, 68, 87, 83].includes(event.keyCode)) {
        keySet.delete(event.keyCode)
    }
}

function addEnemy() {
    if (enemyList.length < 8) {
        let count = 8 - enemyList.length;
        for (let i = 0; i < count; i++) {
            let e = new Enemy();
            enemyList.push(e);
        }
    }
}

//碰撞检测
function checkCrash(a, b) {
    if (a.x + a.width < b.x ||
        b.x + b.width < a.x ||
        a.y + a.height < b.y ||
        b.y + b.height < a.y) {
        return false;
    }
    else {
        return true;
    }
}
//玩家人物与敌机的碰撞检测

function checkHeroAndEnemyCrash() {
    for (let i = enemyList.length - 1; i >= 0; i--) {
        let e = enemyList[i];
        let flag = checkCrash(e, hero);
        if (flag) {
            if (hero.isBoom) {
                //放大招中，持续掉血
                let id = setInterval(() => {
                    e.hp -= (parseInt(Math.random() * 20) + 5);
                    e.isDie();
                    if (!hero.isBoom) {
                        clearInterval(id);
                    }
                }, 200);
            }
            else {
                e.hp -= (parseInt(Math.random() * 20) + 5);
                e.isDie();
            }
        }
    }
}
